<?php
namespace App\Controllers\admin;

/**
 * 管理员管理
 */
class Admintor extends AdminController
{

    /**
     * 列表
     */
    public function index()
    {
        $request = service('request');
        if ($request->isAJAX()) {
            helper('funcs');
            $admintor = model('admintor')->select('id,username,status,realname,mobile,is_super,created_at')->orderBy('id', 'desc');
            $pager = $admintor->pager;
            // 用户名查询
            $data = $this->request->getVar();
            if (! empty($data['username'])) {
                $admintor->like('username', esc($data['username']));
            }
            // 手机号查询
            if (! empty($data['mobile'])) {
                $admintor->like('mobile', $data['mobile']);
            }
            // 真实姓名查询
            if (! empty($data['realname'])) {
                $admintor->like('realname', $data['realname']);
            }
            // 状态查询
            if (! empty($data['status'])) {
                $admintor->where('status', $data['status']);
            }
            $data = $admintor->findAll();
            success($data, count($data));
        }
        return view('admin/admintor/index');
    }

    /**
     * 添加或编辑管理员
     */
    public function edit()
    {
        $request = service('request');
        $admintor = model('admintor');
        $rules = $admintor->getValidationRules([]);
        if ($request->getMethod() == 'post') {
            $id = intval($request->getVar('id'));
            $password = trim($request->getVar('password'));
            $password_confirm = intval($request->getVar('password_confirm'));
            $info = model('admintor')->find($id);
            if ($id > 0 && empty($info)) {
                error('该管理员不存在');
            }
            $rules = $admintor->getValidationRules([]);
            $rules['password_confirm'] = [
                'label' => '密码确认',
                'rules' => 'required|matches[password]'
            ];
            if (empty($info)) {
                $rules['username']['rules'] .= '|is_unique[admintor.username,id]';
            } else {
                $rules['username']['rules'] .= '|is_unique[admintor.username,id,{id}]';
            }
            if (! empty($info) && empty($password)) {
                unset($rules['password']);
            }
            if (! empty($info) && empty($password_confirm)) {
                unset($rules['password_confirm']);
            }
            
            if (! $this->validate($rules)) {
                error($this->validator->listErrors());
            } else {
                $info['status']= intval($request->getVar('status'));
                $info['mobile']= $request->getVar('mobile');
                $info['realname']= $request->getVar('realname');
                $info['desc']= $request->getVar('desc');
                if($id>0){
                    $info['username']=$request->getVar('username');
                }
                $admintor->save($info);
                success();
            }
        }
        error('请求方法错误');
    }

    /**
     * 新增及编辑的表单
     */
    public function edit_form()
    {
        $id = intval($this->request->getVar('id'));
        if ($id > 0) {
            $info = model('admintor')->find($id);
        }
        return view('admin/admintor/edit_form', compact('info'));
    }

    /**
     * 单个和批量删除
     */
    public function batchDelete()
    {
        helper('funcs');
        $data = $this->request->getVar('data');
        if (empty($data) || ! is_array($data)) {
            error('请选择要删除的数据');
        }
        $ids = array_pluck($data, 'id');
        model('admintor')->delete($ids);
        success($ids);
    }
}